package model

import (
	"fmt"
	"github.com/go-redis/redis"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"hepan1/conf"
	"time"
)
var Db *gorm.DB
var RedisDb *redis.Client

func Setup() {
	db, err := gorm.Open("mysql", conf.DataPath)
	if err!=nil{
		panic(err)
	}
	db.SingularTable(true) //表不会默认加s
	db.DB().SetMaxIdleConns(20) //设置连接池
	db.DB().SetMaxOpenConns(100) //最大连接数
	db.DB().SetConnMaxLifetime(time.Second*30)
	db.LogMode(true)
	db.SingularTable(true)
	//Db, _ = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/mysql")
	Db = db
	migration()
	client := redis.NewClient(&redis.Options{
		Addr:     conf.RedisAddr,
		Password: conf.RedisPw,
		DB:       0,  // use default DB
	})
	pong,err := client.Ping().Result()
	fmt.Println(pong, err)
	RedisDb = client
}
func migration(){
	//自动迁移
	Db.Set("gorm:"+conf.DbName,"charset=utf8mb4").
		AutoMigrate(&Drawusers{}).
		AutoMigrate(&Candidatedogs{}).
		AutoMigrate(&Choujiangtokens{}).
		AutoMigrate(&Luckydogs{}).
		AutoMigrate(&Dogsinfor{})
	//DB.Model()
}
